#
# Copyright 2008 Spencer Davis <SpencerDavis91@gmail.com>
#
# Tuxedo is a simple todo list application for GNOME.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://gnu.org/licenses/.>
#

import os
import sys
import gtk
import gtk.glade

class TrayIcon:
    def __init__(self, mainWindow):
        """Creates the tray icon and its context menu"""
        # Stores the main window widget
        self.mainWindow = mainWindow

        # Create the tray icon
        self.trayIcon = gtk.StatusIcon()

        # Set the icon image to Tuxedo.svg
        self.setIcon(os.path.join(os.path.dirname(sys.argv[0]), 'Tuxedo.svg'))

        # Set the corrent path for the glade file 
        gladefile = os.path.join(os.path.dirname(sys.argv[0]), 'TrayMenu.glade')

        # Get widgets from the glade file
        self.widgets = gtk.glade.XML(gladefile)
        
        # Store the signals in a dictionary
        signals = {'on_Add' : self.onAdd,
                   'on_Preferences' : self.onPreferences,
                   'on_Quit' : self.onQuit}

        # Autoconnect the signals
        self.widgets.signal_autoconnect(signals)

        # Store the popup menu widget
        self.popupMenu = self.widgets.get_widget('TrayMenu')

        # Connect clicking on the tray icon to showHideMainWindow
        self.trayIcon.connect('activate', mainWindow.showHideMainWindow)

        # Connect right clicking on the icon to onPopup
        self.trayIcon.connect('popup-menu', self.onPopup, self.popupMenu)

    def onPopup(self, widget, button, time, menu):
        """Show the context menu for the tray icon"""
        menu.show_all()
        menu.popup(None, None, None, button, time)


    def onAdd(self, obj):
        """Creates the dialog to add a new task"""
        # Run MainWindow.onAdd() to create the add dialog
        self.mainWindow.onAdd(None)

    def onPreferences(self, obj):
        """Creates the preferences dialog"""
        # Run MainWindow.onPreferences() to create the preferences dialog
        self.mainWindow.onPreferences(None)

    def onQuit(self, obj):
        """Quits the program"""
        self.mainWindow.onQuit(None)

    def setIcon(self, filename):
        """Changes the icon image to the file given"""
        self.trayIcon.set_from_file(filename)
